<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="white-bg">
	<div class="wrapper wrapper-content animated fadeInRight ibox-content">
		<form class="form-horizontal m" id="form-jobinfo-add">
			<div class="col-md-6">
				<div class="form-group">
					<label class="col-sm-3 control-label">执行器<font color="red">*</font>：
					</label>
					<div class="col-sm-9">
						<select name="jobGroup"  class="form-control"  id="jobGroup" th:with="type=${JobGroupList}">
							<option th:each="JobGroup : ${type}" th:text="${JobGroup.title}" th:value="${JobGroup.id}"></option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">路由策略<font color="red">*</font>：
					</label>
					<div class="col-sm-9">
						<select class="form-control" name="executorRouteStrategy" th:with="type=${ExecutorRouteStrategyEnum}">
						   <option th:each="executorRouteStrategy : ${type}" th:text="${executorRouteStrategy.title}" th:value="${executorRouteStrategy}"></option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">运行模式<font color="red">*</font>：
					</label>
					<div class="col-sm-9">
						<select class="form-control" id="glueType" name="glueType"  th:with="type=${GlueTypeEnum}">
					       <option th:each="glueType : ${type}" th:text="${glueType.desc}" th:value="${glueType}"></option>
				        </select>
					</div>
					 
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">阻塞处理策略<font
						color="red">*</font>：
					</label>
					<div class="col-sm-9">
						<select class="form-control" id="executorBlockStrategy" name="executorBlockStrategy"  th:with="type=${ExecutorBlockStrategyEnum}" >
					       <option th:each="executorBlockStrategy : ${type}" th:text="${executorBlockStrategy.title}" th:value="${executorBlockStrategy}"></option>
				        </select>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">任务超时时间<font
						color="black">*</font>：
					</label>
					<div class="col-sm-9">
						<input type="text" name="executorTimeout" class="form-control"
							placeholder="任务超时时间，单位秒，大于零时生效">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">负责人<font
						color="red">*</font>：
					</label>
					<div class="col-sm-9">
						<input type="text" name="author" class="form-control"
							placeholder="请输入负责人">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">任务参数<font
						color="black">*</font>：
					</label>
					<div class="col-sm-9">
						<textarea name="executorParam" class="form-control"
							placeholder="请输入任务参数"></textarea>
					</div>
				</div>
				
			<!-- <div class="form-group">
					<label class="col-sm-3 control-label">失败处理策略<font
						color="red">*</font>：
					</label>
					<div class="col-sm-9">
						<select class="form-control" id="executorFailStrategy" name="executorFailStrategy"  th:with="type=${ExecutorFailStrategyEnum}">
					       <option th:each="executorFailStrategy : ${type}" th:text="${executorFailStrategy.title}" th:value="${executorFailStrategy}"></option>
				        </select>
					</div>
				</div> -->	
				
			</div>
			
			<div class="col-md-6">
				<div class="form-group">
					<label class="col-sm-3 control-label">任务描述<font color="red">*</font>：
					</label>
					<div class="col-sm-9">
						<input type="text" name="jobDesc" class="form-control"
							placeholder="请输入任务描述">
					</div>
				</div>
				<div class="form-group ui-sortable-helper">
					<label class="col-sm-3 control-label">Cron<font color="red">*</font>：
					</label>
					<div class="col-sm-9">
						<input type="text" name="jobCron" class="form-control"
							placeholder="请输入Cron"><a href="http://www.bejson.com/othertools/cron/" target="blank">在线生成cron</a>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">JobHandler<font
						color="red">*</font>：
					</label>
					<div class="col-sm-9">
						<input type="text" name="executorHandler" class="form-control"
							placeholder="请输入JobHandler">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">子任务ID<font
						color="black">*</font>：
					</label>
					<div class="col-sm-9">
						<input type="text" name="childJobId" class="form-control"
							placeholder="请输入子任务的任务ID,如存在多个则逗号分隔">
					</div>
				</div>
				<div class="form-group ui-sortable-helper">
					<label class="col-sm-3 control-label">失败重试次数<font color="black">*</font>：
					</label>
					<div class="col-sm-9">
						<input type="text" name="executorFailRetryCount" class="form-control"
							placeholder="失败重试次数，大于零时生效" maxlength="4">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">报警邮件<font
						color="black">*</font>：
					</label>
					<div class="col-sm-9">
						<input type="text" name="alarmEmail" class="form-control"
							placeholder="请输入报警邮件，多个邮件地址则逗号分隔">
					</div>
				</div>
				
				<!-- <div class="form-group">
					<label class="col-sm-3 control-label">所属项目<font
						color="red">*</font>：
					</label>
					<div class="col-sm-9">
						<select class="form-control" id="projectId" name="projectId"  th:with="type=${@project.getProject()}">
					       <option th:each="project : ${type}" th:text="${project.projectName}" th:value="${project.projectId}"></option>
				        </select>
					</div>
				</div> -->
			</div>
	<div>
				<input type="hidden" name="glueRemark" value="GLUE代码初始化">
				<textarea name="glueSource" style="display: none;"></textarea>
				<textarea class="glueSource_java" style="display: none;">		
package com.xxl.job.service.handler;

import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;

public class DemoGlueJobHandler extends IJobHandler {

	@Override
	public ReturnT<String> execute(String param) throws Exception {
		XxlJobLogger.log("XXL-JOB, Hello World.");
		return ReturnT.SUCCESS;
	}

}
</textarea>
				<textarea class="glueSource_shell" style="display: none;">
#!/bin/bash
echo "xxl-job: hello shell"

echo "${I18n.jobinfo_script_location}：$0"
echo "${I18n.jobinfo_field_executorparam}：$1"
echo "${I18n.jobinfo_shard_index} = $2"
echo "${I18n.jobinfo_shard_total} = $3"
<!--echo "参数数量：$#"
for param in $*
do
    echo "参数 : $param"
    sleep 1s
done-->

echo "Good bye!"
exit 0
</textarea>
				<textarea class="glueSource_python" style="display: none;">
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import time
import sys

print "xxl-job: hello python"

print "${I18n.jobinfo_script_location}：", sys.argv[0]
print "${I18n.jobinfo_field_executorparam}：", sys.argv[1]
print "${I18n.jobinfo_shard_index}：", sys.argv[2]
print "${I18n.jobinfo_shard_total}：", sys.argv[3]
<!--for i in range(1, len(sys.argv)):
	time.sleep(1)
	print "参数", i, sys.argv[i]-->

print "Good bye!"
exit(0)
<!--
import logging
logging.basicConfig(level=logging.DEBUG)
logging.info("脚本文件：" + sys.argv[0])
-->
</textarea>
				<!--这里有问题，新建一个运行模式为 php 的任务后，GLUE 中没有下边的 php 代码-->
<textarea class="glueSource_php" style="display: none;">
<?php

    echo "xxl-job: hello php  \n";

    echo "${I18n.jobinfo_script_location}：$argv[0]  \n";
    echo "${I18n.jobinfo_field_executorparam}：$argv[1]  \n";
    echo "${I18n.jobinfo_shard_index} = $argv[2]  \n";
    echo "${I18n.jobinfo_shard_total} = $argv[3]  \n";

    echo "Good bye!  \n";
    exit(0);

?>
</textarea>
				<textarea class="glueSource_nodejs" style="display: none;">
#!/usr/bin/env node
console.log("xxl-job: hello nodejs")

var arguments = process.argv

console.log("${I18n.jobinfo_script_location}: " + arguments[1])
console.log("${I18n.jobinfo_field_executorparam}: " + arguments[2])
console.log("${I18n.jobinfo_shard_index}: " + arguments[3])
console.log("${I18n.jobinfo_shard_total}: " + arguments[4])
<!--for (var i = 2; i < arguments.length; i++){
	console.log("参数 %s = %s", (i-1), arguments[i]);
}-->

console.log("Good bye!")
process.exit(0)
</textarea>
				<textarea class="glueSource_powershell" style="display: none;">
Write-Host "xxl-job: hello powershell"

Write-Host "${I18n.jobinfo_script_location}: " $MyInvocation.MyCommand.Definition
Write-Host "${I18n.jobinfo_field_executorparam}: "
	if ($args.Count -gt 2) { $args[0..($args.Count-3)] }
Write-Host "${I18n.jobinfo_shard_index}: " $args[$args.Count-2]
Write-Host "${I18n.jobinfo_shard_total}: " $args[$args.Count-1]

Write-Host "Good bye!"
exit 0
</textarea>
			</div>		
		</form>
	</div>
	<div th:include="include::footer"></div>
	<script type="text/javascript">
		var prefix = ctx + "job/jobinfo";
		$("#form-jobinfo-add").validate({
			rules : {
				jobDesc : {
					required : true,
					maxlength: 50
				},
	            jobCron : {
	            	required : true
	            },
				author : {
					required : true
				},
	            executorTimeout : {
	                digits:true
	            },
	            executorFailRetryCount : {
	            	 digits:true
	            }
	        }, 
	        messages : {  
	            jobDesc : {
	            	required : "请输入任务描述"
	            },
	            jobCron : {
	            	required :  "请输入Cron"
	            },
	            author : {
	            	required : "请输入负责人"
	            },
	            executorTimeout : {
	                digits: '请输入整数'
	            },
	            executorFailRetryCount : {
	            	 digits: '请输入整数'
	            }
	        }
		});

		function submitHandler() {
			if ($.validate.form()) {
				 var executorTimeout = $("#form-jobinfo-add input[name='executorTimeout']").val();
				 if(!/^\d+$/.test(executorTimeout)) {
					 executorTimeout = 0;
		            }
	            $("#form-jobinfo-add input[name='executorTimeout']").val(executorTimeout);
				 var executorFailRetryCount = $("#form-jobinfo-add input[name='executorFailRetryCount']").val();
				 if(!/^\d+$/.test(executorFailRetryCount)) {
		                executorFailRetryCount = 0;
		            }
	            $("#form-jobinfo-add input[name='executorFailRetryCount']").val(executorFailRetryCount);
	            
	            var glueType = $("#glueType").val();
	            if ('GLUE_GROOVY'==glueType){
	    			$("#form-jobinfo-add textarea[name='glueSource']").text( $("#form-jobinfo-add  .glueSource_java").text() );
	    		} else if ('GLUE_SHELL'==glueType){
	    			$("#form-jobinfo-add  textarea[name='glueSource']").text( $("#form-jobinfo-add  .glueSource_shell").text() );
	    		} else if ('GLUE_PYTHON'==glueType){
	    			$("#form-jobinfo-add  textarea[name='glueSource']").text( $("#form-jobinfo-add  .glueSource_python").text() );
	    		} else if ('GLUE_PHP'==glueType){
	                $("#form-jobinfo-add  textarea[name='glueSource']").text( $("#form-jobinfo-add  .glueSource_php").text() );
	            } else if ('GLUE_NODEJS'==glueType){
	    			$("#form-jobinfo-add  textarea[name='glueSource']").text( $("#form-jobinfo-add  .glueSource_nodejs").text() );			
	    		} else if ('GLUE_POWERSHELL'==glueType){
	                $("#form-jobinfo-add  textarea[name='glueSource']").text( $("#form-jobinfo-add  .glueSource_powershell").text() );
	            } else {
	                $("#form-jobinfo-add  textarea[name='glueSource']").text("");
	    		}
				$.operate.save(prefix + "/add",$('#form-jobinfo-add').serialize());
			}
		}
	
		function init() {
			var $executorHandler = $("#glueType").parents("form").find(
					"input[name='executorHandler']");
			var glueType = $("#glueType").val();
			if ('BEAN' != glueType) {
				$executorHandler.val("");
				$executorHandler.attr("readonly", "readonly");
			} else {
				$executorHandler.removeAttr("readonly");
			}
		}
		$("#glueType").change(function() {
			init();
		});
		
		
	</script>
</body>
</html>
